<?php

declare (strict_types=1);
/**
 * @copyright 深圳市易果网络科技有限公司
 * @version 1.0.0
 * @link https://dayiguo.com
 */

namespace App\Controller\agent;

use App\Dao\LocationDao;
use App\Kernel\Util\AgentJwtInstance;
use App\Request\Agent\User\AgentShareRequest;
use App\Service\Agent\StoreService;

use Hyperf\Guzzle\ClientFactory;
use App\Middleware\User\AgentMiddleware;
use App\Request\Agent\User\UserInfoRequest;
use App\Request\Agent\User\ChangePasswordRequest;
use App\Request\Agent\User\UpdatePayPasswordRequest;
use App\Request\Agent\User\UserPayPasswordRequest;
use App\Request\Agent\User\retrievePhoneRequest;

use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\Snowflake\IdGeneratorInterface;
use Hyperf\HttpServer\Annotation\Middleware;

/**
 * 代理商后台用户控制器
 *
 * @AutoController()
 * @Middleware(AgentMiddleware::class)
 * @author 张昊(2396675557@qq.com)
 * @package App\Controller\admin
 */
class UserController extends Controller
{

    /**
     * 代理商后台用户服务
     * @Inject
     * @var StoreService
     */
    private $store_service;

    /**
     * @Inject
     * @var LocationDao
     */
    private $location_dao;

    /**
     * 获取用户平台信息
     */
    public function info()
    {
        $user = AgentJwtInstance::instance()->build()->getAgent();

        $data['logo'] = $user['logo'];
        $data['name'] = $user['name'];
        $data['phone'] = $user['phone'];
        $data['identity'] = $user['identity'];
        $data['corporate_name'] = $user['corporate_name'];

        $this->success($data);

    }

    /**
     * update
     *
     * @param UserInfoRequest $request
     */
    public function update(UserInfoRequest $request)
    {
        $params = $request->all();

        $field_all = array_intersect(array_keys($params), ['name', 'logo', 'corporate_name']);

        foreach ($field_all as $field) {
            $data[$field] = $params[$field];
        }

        $update = $this->store_service->agent_dao->update($data);

        !$update && $this->error();

        $this->success();
    }

    /**
     * 修改密码接口
     *
     * @param ChangePasswordRequest $request
     */
    public function change_password(ChangePasswordRequest $request)
    {
        $new_password = $request->input('old_password');
        $password = $request->input('password');

        $user = AgentJwtInstance::instance()->build()->getAgent();

        //验证密码
        if( !password_verify( $new_password, $user->login_pwd ) ) {
            $this->error('USER.OLD_PASSWORD_ERRORS');
        }

        $update = $this->store_service->agent_dao->update([
            'login_pwd' => password_hash($password, PASSWORD_DEFAULT )
        ]);

        !$update && $this->error();

        $this->success();
    }

    /**
     * 判断用户是否有支付密码
     */
    public function is_safe_pwd()
    {
        $user = AgentJwtInstance::instance()->build()->getAgent();

        $result = 0;

        if( !empty($user->safe_pwd) ) {
            $result = 1;
        }

        $this->success($result);
    }

    /**
     *添加支付密码
     *
     * @param UserPayPasswordRequest $request
     */
    public function setPayPassword(UserPayPasswordRequest $request)
    {
        $params = $request->all();

        $update = $this->store_service->agent_dao->update([
            'safe_pwd' => password_hash($params['password'], PASSWORD_DEFAULT )
        ]);

        !$update && $this->error();

        $this->success();
    }

    /**
     *修改支付密码
     *
     * @param UpdatePayPasswordRequest $request
     */
    public function updatePayPassword(UpdatePayPasswordRequest $request)
    {
        $new_password = $request->input('old_password');
        $password = $request->input('password');

        $user = AgentJwtInstance::instance()->build()->getAgent();

        //验证密码
        if( !password_verify( $new_password, $user->login_pwd ) ) {
            $this->error('USER.OLD_PASSWORD_ERRORS');
        }

        $update = $this->store_service->agent_dao->update([
            'safe_pwd' => password_hash($password, PASSWORD_DEFAULT )
        ]);

        !$update && $this->error();

        $this->success();
    }

    /**
     * 找回密码 - 验证手机
     *
     * @param retrievePhoneRequest $request
     */
    public function retrievePhone ( retrievePhoneRequest $request )
    {
        $params = $request->all();

        isset($params['phone']) && $params['phone'] = trim($params['phone']);
        isset($params['code']) && $params['code'] = trim($params['code']);

        $this->store_service->retrievePhone($params['phone'], $params['code']);

        $this->success();
    }

    /**
     * 查询代理区域
     */
    public function region()
    {

        $agent = AgentJwtInstance::instance()->build()->getAgent();

        $map = [
            'level' => $agent['location_id']
        ];
        switch ( $agent['identity'] ) {
            case "2":
                $map = [
                    'level' => $agent['location_id']
                ];
                break;
        }
        $result = $this->location_dao->getLocationsByMap($map);

        $this->success($result);
    }

    /**
     * 获取分公司直招区代理代理区域
     */
    public function getSubordinate()
    {
        $result = $this->store_service->getSubordinate();

        $this->success($result);
    }

    /**
     * 获取分公司直招合伙人招的区代
     */
    public function getPartnerArea()
    {
        $result = $this->store_service->getPartnerArea();

        $this->success($result);
    }

    /**
     * 获取代理商\合伙人分润
     */
    public function getAgentShare()
    {
        $result = $this->store_service->getAgentShare();

        $this->success($result);
    }

    /**
     * 通过ID获取合伙人分润比例
     */
    public function getAgentShareById()
    {
        $partner_id = (int)$this->request->input('partner_id', 0);

        $result = $this->store_service->getAgentShareById($partner_id);

        $this->success($result);
    }

    /**
     * update
     *
     * @param AgentShareRequest $request
     */
    public function updateAgentShare(AgentShareRequest $request)
    {
        $params = $request->all();

        $field_all = array_intersect(array_keys($params), ['area_agent_money', 'city_agent_money', 'channel_agent_money']);

        foreach ($field_all as $field) {
            $data[$field] = $params[$field];
        }

        $update = $this->store_service->agent_dao->update($data);

        !$update && $this->error();

        $this->success();
    }
}